//
//  SendCardAlert.m
//  ChangChat
//
//  Created by Cocoa on 2019/9/5.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "SendCardAlert.h"

@implementation SendCardAlert

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    self.backgroundColor = [UIColor blackColorWithAlpha:0.3];
    self.pContentView = [UIView new];
    self.pContentView.layer.cornerRadius = 5.0;
    self.pContentView.backgroundColor = UIColor.whiteColor;
    self.sendToLabel = [UILabel k_labelWithText:@"发送给:" boldFontSize:15 textColor:UIColor.k_t2Color];
    self.iconImgView = UIImageView.new;
    self.nameLabel = [UILabel k_labelWithFontSize:15 textColor:UIColor.k_t2Color];
    self.cardLabel = [UILabel k_labelWithFontSize:13 textColor:UIColor.k_t3Color];
    UIView *line1 = [UIView k_lineView];
    UIView *line2 = [UIView k_lineView];
    self.cancelBtn = UIButton.k_initType(UIButtonTypeCustom).k_bgImgColor(UIColor.whiteColor).k_title(@"取消").k_titleColor(UIColor.k_t2Color);
    self.sendBtn = UIButton.k_initType(UIButtonTypeCustom).k_bgImgColor(UIColor.whiteColor).k_title(@"确认").k_titleColor(UIColor.k_t1Color);
    
    [self addSubview:self.pContentView];
    [self.pContentView addSubview:self.sendToLabel];
    [self.pContentView addSubview:self.iconImgView];
    [self.pContentView addSubview:self.nameLabel];
    [self.pContentView addSubview:self.cardLabel];
    [self.pContentView addSubview:line1];
    [self.pContentView addSubview:line2];
    [self.pContentView addSubview:self.cancelBtn];
    [self.pContentView addSubview:self.sendBtn];
    @weakify(self);
    [[self.cancelBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self hide];
    }];
    
    [self.sendToLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(20);
        make.top.mas_offset(20);
        make.right.mas_offset(-15);
    }];
    
    [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.sendToLabel);
        make.top.equalTo(self.sendToLabel.mas_bottom).offset(15);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
    self.iconImgView.layer.masksToBounds = YES;
    self.iconImgView.layer.cornerRadius = 25;
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.iconImgView);
        make.left.equalTo(self.iconImgView.mas_right).offset(10);
        make.right.mas_offset(-15);
    }];
    self.nameLabel.numberOfLines = 2;
    
    [self.cardLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.sendToLabel);
        make.top.equalTo(self.iconImgView.mas_bottom).offset(10);
        make.right.mas_offset(-15);
    }];
    
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(self.cardLabel.mas_bottom).offset(10);
        make.height.mas_equalTo(1);
    }];
    
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.top.equalTo(line1.mas_bottom);
        make.height.mas_equalTo(35);
        make.width.mas_equalTo(1);
        make.bottom.mas_offset(0);
    }];
    
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.right.equalTo(line2.mas_left);
        make.top.equalTo(line2);
        make.bottom.equalTo(line2);
    }];
    
    [self.sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line2.mas_right);
        make.right.mas_offset(0);
        make.top.equalTo(line2);
        make.bottom.equalTo(line2);
    }];
    
    [self.pContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(50);
        make.right.mas_offset(-50);
        make.center.equalTo(self);
    }];
}

- (void)showWithContent:(NSString *)content toUserName:(NSString *)userName toUserAvatar:(NSString *)toUserAvatar {
    self.frame = ScreenBounds;
    
    [self.iconImgView sd_setImageWithURL:toUserAvatar.toURL placeholderImage:UIImage.k_imgName(@"placeholder_user")];
    self.nameLabel.text = userName;
    self.cardLabel.text = content;
    
    [kWindow addSubview:self];
}
- (void)hide {
    [self removeFromSuperview];
}

@end
